package {{packageName}}.server.router.impl.sys

import com.firefly.annotation.Component
import com.firefly.annotation.Inject
import com.firefly.codec.http2.model.HttpMethod
import com.firefly.kotlin.ext.http.HttpServer
import {{packageName}}.server.router.RouterInstaller
import {{packageName}}.server.router.impl.sys.handler.*
import com.firefly.server.http2.router.RouterManager

/**
 * @author Pengtao Qiu
 */
@Component("sysRouter")
class SystemRouter : RouterInstaller {

    @Inject
    private lateinit var server: HttpServer

//    @Inject
//    private lateinit var redisSessionHandler: RedisSessionHandler

    @Inject
    private lateinit var staticResourceHandler: StaticResourceHandler

    @Inject
    private lateinit var globalHandler: GlobalHandler

    @Inject
    private lateinit var transactionalHandler: TransactionalHandler

    @Inject
    private lateinit var errorHandler: ErrorHandler

    override fun install() = server.addRouters {

        // session handler
//        router {
//            path = "*"
//            asyncHandler { redisSessionHandler.handle(this) }
//        }

        // static file handler
        router {
            httpMethod = HttpMethod.GET
            paths = staticResourceHandler.staticResources
            asyncHandler(staticResourceHandler)
        }

        // global handler
        router {
            path = "*"
            asyncHandler(globalHandler)
        }

        // transactional handler
        router {
            httpMethods = listOf(HttpMethod.POST, HttpMethod.PUT, HttpMethod.DELETE)
            paths = listOf("/project/save.json")
            asyncHandler(transactionalHandler)
        }

        // error handler
        router(RouterManager.DEFAULT_LAST_ROUTER_ID + 1) {
            path = "*"
            asyncHandler(errorHandler)
        }
    }

    override fun order() = 0
}